package com.sikaryofficial.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sikaryofficial.backend.domain.dto.req.points.PointsDetailQueryReq;
import com.sikaryofficial.backend.domain.dto.resp.points.PointsDetailResp;
import com.sikaryofficial.backend.manager.PointsBalanceManager;
import com.sikaryofficial.backend.service.IPointsDetailStrategy;
import com.sikaryofficial.backend.service.points.cache.PointsRuleConfigCacheService;
import com.sikaryofficial.common.core.constant.PointRuleConstant;
import com.sikaryofficial.common.core.constant.SecurityConstants;
import com.sikaryofficial.common.core.domain.R;
import com.sikaryofficial.common.core.utils.PageUtil;
import com.sikaryofficial.common.core.web.controller.BaseController;
import com.sikaryofficial.common.core.web.domain.PageResult;
import com.sikaryofficial.system.api.model.ScanRegisterVO;
import com.sikaryofficial.system.api.model.ScanTaskVO;
import com.sikaryofficial.system.api.model.UserPointResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 积分余额Controller
 *
 * @author qinjinyuan
 * @date 2023-10-27
 */
@RestController
@RequestMapping("/pointsBalance")
@Api(tags = "积分余额")
@Slf4j
public class PointsBalanceController extends BaseController {
    @Autowired
    private PointsBalanceManager pointsBalanceManager;
    @Resource
    private Map<String, IPointsDetailStrategy> pointsDetailStrategyMap;
    @Resource
    private PointsRuleConfigCacheService ruleConfigCacheService;
    /**
     * 积分流水接口
     *
     * @return 积分流水分页数据
     */
    @PostMapping("/pointFlow")
    @ApiOperation("积分流水分页列表")
    public PageResult<PointsDetailResp> pointFlow(@Validated @RequestBody PointsDetailQueryReq req) {

        IPage<PointsDetailResp> page = pointsBalanceManager.pointFlow(req);
        return PageUtil.success(page.getRecords(), page.getTotal());
    }

    /**
     * 扫描注册积分处理
     *
     * @param scanRegisterVO 扫码注册信息
     * @return
     */
    @PostMapping("/scanRegister")
    @ApiOperation("扫描注册积分处理")
    public R<Boolean> scanRegister(@RequestBody ScanRegisterVO scanRegisterVO, @RequestHeader(name = SecurityConstants.BRAND_KEY, required = false) String brand) {
        return R.ok(pointsBalanceManager.scanRegister(scanRegisterVO, brand));
    }

    /**
     * 扫码任务积分处理
     *
     * @return
     */
    @PostMapping("/scanTask")
    @ApiOperation("扫码任务积分处理")
    public R<UserPointResultVO> scanTask(@RequestBody ScanTaskVO scanTaskVO) {
        // 扫码任务获取积分
        IPointsDetailStrategy scanCodeStrategy = pointsDetailStrategyMap.get(PointRuleConstant.SCAN_TASK + IPointsDetailStrategy.SUFFIX);
        boolean flag = scanCodeStrategy.updatePointsByInputValue(scanTaskVO.getBrandTasteId(), scanTaskVO.getUserId(), null, scanTaskVO.getParam(), "");
        log.info("扫码任务积分处理结果：入参{},结果{}", scanTaskVO,flag);
        UserPointResultVO data = UserPointResultVO.builder().getPointsFlag(false).pointsDetailValue(0).build();
        Integer pointsDetailValue = ruleConfigCacheService.getConfigByCode(PointRuleConstant.SCAN_TASK).getPointsValue();
        if (Boolean.TRUE.equals(flag)) {
            data.setPointsDetailValue(pointsDetailValue);
        }
        data.setGetPointsFlag(flag);
        log.info("扫码任务积分处理结果data：{}", data);
        return R.ok(data);
    }

    /**
     * reset  Unused points email
     * <p>
     * Unused points will be automatically reset to zero on the last day of each year.
     *
     * @return
     */
    @GetMapping("/autoSendResetEmailNotice")
    @ApiOperation("积分重置邮件通知")
    public R<Boolean> autoSendResetEmailNotice() {
        return R.ok(pointsBalanceManager.autoSendResetEmailNotice());
    }

    /**
     * Unused points will be automatically reset to zero on the last day of each year.
     * 2023-12-31 23:59:59
     *
     * @return
     */
    @GetMapping("/autoResetUnusedPoints")
    @ApiOperation("自动清除积分")
    public R<Boolean> autoResetUnusedPoints() {
        // 查询获奖列表
        return R.ok(pointsBalanceManager.autoResetUnusedPoints());
    }
}
